package com.example.financecollegeorderdish.ui

import android.util.Log
import androidx.activity.viewModels
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.example.financecollegeorderdish.databinding.ActivitySplashBinding
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.EmptyViewModel
import com.wuhao.lib_base.mvvm.v.BaseActivity
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

class SplashActivity : BaseActivity<ActivitySplashBinding, EmptyViewModel>() {
    override val mViewModel: EmptyViewModel by viewModels()
    private val targetClass: Class<*> = MainActivity::class.java
//    private val targetClass: Class<*> = TargetDetailActivity::class.java


    override fun createVB(): ActivitySplashBinding = ActivitySplashBinding.inflate(layoutInflater)

    override fun initObserve() {

    }

    override fun initRequestData() {
        getScreenInfo()
        requestAllPermission()
//        startCountDown()
    }

    override fun ActivitySplashBinding.initView() {

    }

    /**
     * 请求所有权限
     */
    private fun requestAllPermission() {
        /**
         * 申请摄像头和存储权限
         */
        XXPermissions.with(this)
            .permission(
//                Permission.CAMERA,
                Permission.MANAGE_EXTERNAL_STORAGE
            )
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
                        showWarning("获取部分权限成功，但部分权限未正常授予")
                    }
                    startCountDown()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
                        showWarning("注意：被永久拒绝授权，会影响系统正常使用")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(this@SplashActivity, permissions)
                    } else {
                        showWarning("系统获取权限失败，会影响系统正常使用")
                    }
                }
            })
    }

    /**
     * 倒计时两秒进入登录界面
     *
     * @return
     */
    private fun startCountDown(): Disposable {
        return Observable.timer(2, TimeUnit.SECONDS)
            .subscribe { aLong: Long? ->
                startActivity(
                    targetClass,
                    true
                )
            }
    }

    /**
     * 获取屏幕信息
     */
    fun getScreenInfo() {
        Log.d("MainActivity", "屏幕的宽度（单位：px）: ${ScreenUtils.getScreenWidth()}")
        Log.d("MainActivity", "屏幕的高度（单位：px）: ${ScreenUtils.getScreenHeight()}")
        Log.d(
            "MainActivity",
            "屏幕的宽度（单位：dp）: ${SizeUtils.px2dp(ScreenUtils.getScreenWidth().toFloat())}"
        )
        Log.d(
            "MainActivity",
            "屏幕的高度（单位：dp）: ${SizeUtils.px2dp(ScreenUtils.getScreenHeight().toFloat())}"
        )
        Log.d("MainActivity", "手机屏幕密度:${ScreenUtils.getScreenDensity()}")
        Log.d("MainActivity", "手机屏幕密度 DPI:${ScreenUtils.getScreenDensityDpi()}")
    }
}